package com.synchrona.pluginManager;

import java.io.IOException;

import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.Query;
import com.google.gdata.client.blogger.BloggerService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextContent;
import com.google.gdata.util.ServiceException;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import com.synchrona.account.Account;

import java.io.IOException;
import java.net.URL;

import org.apache.http.auth.AUTH;

public class Blogger {
	private static final String METAFEED_URL = "http://www.blogger.com/feeds/default/blogs";
	private static final String FEED_URI_BASE = "http://www.blogger.com/feeds";
	private static final String POSTS_FEED_URI_SUFFIX = "/posts/default";
	private static final String COMMENTS_FEED_URI_SUFFIX = "/comments/default";
	private static String feedUri;

	private static BloggerService myService = null;

	public Blogger() {
		myService = new BloggerService("exampleCo-exampleApp-1");
	}

	public String getBlogURL(String blogTitle, Account account) {
		String url = null;
		try {
			System.out.println("Got the service blogger..");
			url = getURL(blogTitle, account);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
	

	private String getBlogId(BloggerService myService) {
		String field = null;
		try {
			final URL feedUrl = new URL(METAFEED_URL);
			Feed resultFeed = (Feed) myService.getFeed(feedUrl, Feed.class);
			if (resultFeed.getEntries().size() > 0) {
				Entry entry = (Entry) resultFeed.getEntries().get(0);
				field = entry.getId().split("blog-")[1];
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return field;
	}

	public BloggerService authenticate(Account account){
		try {
			String userName = account.getAccountEmail();
			String userPassword = account.getServicePassword();
			myService.setUserCredentials(userName, userPassword);
			System.out.println("Entered blogger..");
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return myService;
	}
	
	public String getURL(String blogTitle, Account account) {
		String url = null;
		try {

			myService = authenticate(account);
			Boolean FALSE = new Boolean(false);

			String blogId = getBlogId(myService);
			feedUri = FEED_URI_BASE + "/" + blogId;
			System.out.println("bogID :" + blogId);

			url = getLastPost(myService);

			System.out.println("Got the service blogger..");
			String lastId = getLastPostId(myService);
			String[] ids = lastId.split("-");
			String postId = ids[2];
			System.out.println(postId);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}

	public String getLastPost(BloggerService myService)
			throws ServiceException, IOException {
		String blogId = getBlogId(myService);
		feedUri = FEED_URI_BASE + "/" + blogId;
		URL feedUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);
		Feed resultFeed = (Feed) myService.getFeed(feedUrl, Feed.class);
		System.out.println(resultFeed.getTitle().getPlainText());
		Entry entry = (Entry) resultFeed.getEntries().get(0);
		String feerl = entry.getHtmlLink().getHref().toString();
		System.out.println(feerl);
		return feerl;
	}

	public String getLastPostId(BloggerService myService) {
		String postID = null;
		try {
			String blogId = getBlogId(myService);
			feedUri = FEED_URI_BASE + "/" + blogId;
			URL feedUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);
			Feed resultFeed = (Feed) myService.getFeed(feedUrl, Feed.class);
			System.out.println(resultFeed.getTitle().getPlainText());
			Entry entry = (Entry) resultFeed.getEntries().get(0);
			postID = entry.getId();
			System.out.println(postID);
			System.out.println(entry.getTitle());
			
			String[] ids = postID.split("-");
			postID = ids[2];
			System.out.println("Last PostId: " + postID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return postID;
	}

	public void getLastBlogComments(Account account)
			throws ServiceException, IOException {
		String postId = null;
		String blogId = null;
		try {
			myService = authenticate(account);
			blogId = getBlogId(myService);
			postId = getLastPostId(myService);
			feedUri = FEED_URI_BASE + "/" + blogId;
			if(postId == null){
				System.out.println("No posts in the blog");
			}
			else{
				// Build comment feed URI and request comments on the specified post
				String commentsFeedUri = feedUri + "/" + postId
						+ COMMENTS_FEED_URI_SUFFIX;
				//String commentsFeedUri = feedUri + COMMENTS_FEED_URI_SUFFIX;
				URL feedUrl = new URL(commentsFeedUri);
				Feed resultFeed = (Feed) myService.getFeed(feedUrl, Feed.class);
				if(resultFeed.getEntries().size() == 0){
					System.out.println("No Comments on this post!!!");
					return;
				}
				else{
				// Display the results
					System.out.println(resultFeed.getTitle().getPlainText());
					for (int i = 0; i < resultFeed.getEntries().size(); i++) {
						Entry entry = (Entry) resultFeed.getEntries().get(i);
						System.out.println("\t" + ((TextContent) entry.getContent()).getContent()
										.getPlainText());
						System.out.println("\t" + entry.getUpdated().toStringRfc822());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
